package com.yang.article.listener;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.yang.article.mapper.ApArticleConfigMapper;
import com.yang.common.constants.WmNewsMessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: Billyang
 * @date: 2024/10/4 17:49
 */
@Component
@Slf4j
public class ArticleUpOrDownListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message) {
        log.info("article端文章上下架监听到消息，message={}", message);
        if (StringUtils.isNotBlank(message)) {
            Map map = JSON.parseObject(message, Map.class);
            String enable = map.get("enable").toString();
            String articleId = map.get("articleId").toString();
            apArticleConfigMapper.updateEnableByArticleId(Long.valueOf(articleId), Boolean.valueOf(enable));
            log.info("article端文章配置修改，articleId={}", articleId);
        }
    }
}
